/////////////////////////////////////////////////////////////////////////////
// Name:        ax_menu_text.cpp
// Purpose:     Modal text prompt menu class
// Author:      Julian Smart
// Modified by:
// Created:     2009-03-20
// RCS-ID:
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "ax/ax_menu_text.h"
#include "ax/ax_textinputdialog.h"
#include "ax/ax_view.h"

/*
 * AxTextActivator implementation.
 * Sets the value of a boolean variable when toggled.
 */

IMPLEMENT_DYNAMIC_CLASS( AxTextActivator, AxAutoUpdateActivator )

AxTextActivator::AxTextActivator(
        const wxString& labelSpec,
        const wxString& descrSpec)
{
    Init();

    m_labelSpec = labelSpec;
    m_descriptionSpec = descrSpec;
}

void AxTextActivator::Init()
{
}

AxTextMenuItem* AxTextActivator::GetTextMenuItem() const
{
    return wxDynamicCast(GetMenuItem(), AxTextMenuItem);
}

void AxTextActivator::Copy(const AxTextActivator& activator)
{
    AxAutoUpdateActivator::Copy(activator);
}

AxMenu* AxTextActivator::Activate(AxMenuController* WXUNUSED(controller), AxMenuItem* WXUNUSED(item))
{
    AxTextMenuItem* textItem = GetTextMenuItem();
    if (!textItem)
        return NULL;

    wxString oldValue(textItem->GetValue());

    int dialogStyle = wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX|wxTAB_TRAVERSAL;
    if (textItem->GetStyle() & axMENU_STYLE_MULTILINE_TEXT)
        dialogStyle |= axTEXTDIALOG_MULTILINE;

    AxTextInputDialog dialog(NULL, textItem->GetMessage(), textItem->GetCaption(), oldValue,
        wxDefaultPosition, wxDefaultSize, dialogStyle);
    
    if (AxViewManager::Get())
    {
        dialog.SetSpeech(& AxViewManager::Get()->GetSpeech());
        dialog.SetShortcutManager(AxViewManager::Get()->GetShortcutManager());
    }

    if (dialog.ShowModal() == wxID_OK && oldValue != dialog.GetValue())
    {
        textItem->SetValue(dialog.GetValue());
        textItem->SendTextUpdate(dialog.GetValue());
    }

    return NULL;
}

wxString AxTextActivator::CreateString(const wxString& spec, bool isLabel)
{
    AxTextMenuItem* textItem = GetTextMenuItem();
    if (textItem)
        return textItem->CreateString(spec, isLabel);
    else
        return wxEmptyString;
}

/*
 * AxTextMenuItem class declaration.
 */

IMPLEMENT_DYNAMIC_CLASS( AxTextMenuItem, AxMenuItem )

AxTextMenuItem::AxTextMenuItem(const wxString& label,
        const wxString& description,
        const wxString& id,
        const wxString& caption,
        const wxString& message,
        const wxString& value,
        int style)
{
    Create(label, description, id, caption, message, value, style);
}

bool AxTextMenuItem::Create(const wxString& label,
        const wxString& description,
        const wxString& id,
        const wxString& caption,
        const wxString& message,
        const wxString& value,
        int style)
{
    SetStyle(style);
    SetKind(_("text"));
    SetLabel(label);
    SetDescription(description);
    SetId(id);
    SetDynamic();

    m_message = message;
    m_caption = caption;
    m_value = value;

    AxTextActivator* activator = new AxTextActivator(label, description);
    SetActivator(activator);

    return true;
}

// Set value
void AxTextMenuItem::SetValue(const wxString& value)
{
    m_value = value;

    TransferDataFromMenu();
}

void AxTextMenuItem::SendTextUpdate(const wxString& value)
{
    wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, AxGetId(GetId()));
    event.SetEventObject(this);
    event.SetString(value);

    ProcessEvent(event);
}

wxString AxTextMenuItem::CreateString(const wxString& spec, bool WXUNUSED(isLabel))
{
    wxString value = m_value;
    if (value.IsEmpty())
        value = _("none");
    wxString str = spec;
    str.Replace(wxT("%VALUE%"), value);
    return str;
}

IMPLEMENT_DYNAMIC_CLASS( AxTextValidator, AxValidator )

// Override to pass a value from the UI to a data structure
bool AxTextValidator::TransferDataFromMenu()
{
    AxTextMenuItem* textItem = wxDynamicCast(GetMenuItem(), AxTextMenuItem);
    if (textItem)
    {
        if (m_stringPtr)
            * m_stringPtr = textItem->GetValue();
    }

    return true;
}

// Override to pass a value from a data structure to the UI
bool AxTextValidator::TransferDataToMenu()
{
    AxTextMenuItem* textItem = wxDynamicCast(GetMenuItem(), AxTextMenuItem);
    if (textItem)
    {
        if (m_stringPtr)
            textItem->SetValue(* m_stringPtr);
    }

    return true;
}
